import urllib.request
from lxml import etree

def create_request(page):
    url = 'https://www.kelagirls.com/albums-8-page-'+str(page)+'.html'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
    }
    request = urllib.request.Request(url=url,headers=headers)
    return request

def get_content(request):
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    return content

def down_load(content):
    tree = etree.HTML(content)
    name_list = tree.xpath('//div[@class]/div[@class="dd-up"]/span[2]/text()')
    img_list = tree.xpath('//div[@class]//img[@class="album-img"]/@src')
    for i in range(len(name_list)):
        name = name_list[i]
        img = img_list[i]
        url = '' + img
        urllib.request.urlretrieve(url=url,filename= name +  '.jpg')






if __name__ == '__main__':
    start_page = int(input('请输入起始页码:'))
    end_page = int(input('请输入终止页码:'))
    for page in range(start_page,end_page+1):
        request = create_request(page)
        content  = get_content(request)
        down_load(content)

